﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Web.Mvc;
using Sendz.Validators;

namespace Sendz.Domain
{
    public class Form
    {
        // Entity
        [HiddenInput(DisplayValue = false)]
        public Guid FormKey { get; set; }

        [DisplayName("Short Path")]
        [StringLength(12, ErrorMessage = "If you are going to use a short path for your form, it must be less than 12 characters")]
        public string ShortPath { get; set; }

        [DisplayName("Form Name")]
        [Required(ErrorMessage = "A name for your form is required")]
        [StringLength(50, ErrorMessage = "Please ensure that the form name you enter is less than 50 characters")]
        public string Name { get; set; }

        [DisplayName("Redirect When Complete?")]
        public bool IsRedirectOnComplete { get; set; }

        [DisplayName("Completion Message")]
        [DataType(DataType.MultilineText)]
        [RequiredIf("IsRedirectOnComplete", false, ErrorMessage = "The form needs a confirmation message upon completion, unless it redirects")]
        public string CompletionMessage { get; set; }

        [DisplayName("Redirect URL")]
        [DataType(DataType.Url)]
        [RequiredIf("IsRedirectOnComplete", true, ErrorMessage="If the form will redirect upon completion, you must specify a redirect URL")]
        [RegularExpression(@"^(http|https|ftp)\://[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(:[a-zA-Z0-9]*)?/?([a-zA-Z0-9\-\._\?\,\'/\\\+&amp;%\$#\=~])*[^\.\,\)\(\s]$", ErrorMessage = "Please enter a valid redirect URL (e.g. http://www.domain.com)")]
        [StringLength(255, ErrorMessage = "Please ensure that the redirect URL you enter is less than 255 characters")]
        public string RedirectURL { get; set; }

        [HiddenInput(DisplayValue = false)]
        public bool IsRemoved { get; set; }

        [HiddenInput(DisplayValue = false)]
        public DateTime? LastModified { get; set; } // Nullable, a form may be created but not modified yet
        
        [HiddenInput(DisplayValue = false)]
        public DateTime CreatedOn { get; set; }

        [DisplayName("Send an Alert?")]
        public bool AlertOnComplete { get; set; }

        // Parents
        [HiddenInput(DisplayValue = false)]
        public Guid UserKey { get; set; }
        public User User { get; set; }

        // (Optional)

        [DisplayName("AutoResponder")]
        public Guid? AutoResponderKey { get; set; } // Nullable, a form doesn't have to have an autoresponder

        [HiddenInput(DisplayValue = false)]
        public AutoResponder AutoResponder { get; set; }

        // Children
        [HiddenInput(DisplayValue = false)]
        public List<FormField> FormFields { get; set; }
                
        [HiddenInput(DisplayValue = false)]
        public List<Response> Responses { get; set; }

        // Constructors
        public Form()
        {
            // Note: The database can create these if StoreGeneratedPattern is set to Computed in the Entity model
            // Will most likely use Computed when newsequentialid() is used in SQL for index performance on transactional tables
            this.FormKey = Guid.NewGuid();
            this.CreatedOn = DateTime.Now;

            this.FormFields = new List<FormField>();
        }

        public Form(Guid userKey)
        {
            this.FormKey = Guid.NewGuid();
            this.UserKey = userKey;
            this.CreatedOn = DateTime.Now;
        }

        public Form(string name, bool isRedirectOnComplete, string completionMessage, string redirectURL)
        {
            this.FormKey = Guid.NewGuid();
            this.Name = name;
            this.IsRedirectOnComplete = isRedirectOnComplete;
            this.CompletionMessage = completionMessage;
            this.RedirectURL = redirectURL;
            this.CreatedOn = DateTime.Now;
        }
    }
}
